﻿using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.DataModel.CommonManageModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace LdtDataEngin.ModuleDetail.RegCommon.SourceDataAnalysis
{
    ///<summary>
    /// 类名称  : ExtractSourceElement
    /// 功能    : 要素中心数据提取使用
    /// 更新人  : ljx 
    ///</summary> 
    public class ExtractSourceElement : IExtractSource
    {
        public DataContext dataContext { get; set; }

        /// <summary>
        /// 案件要素中心数据源配置获取
        /// </summary>
        /// <param name="extractSource">数据源表配置对象</param>
        /// <param name="parentOriginalTxtList">父级原文内容</param>
        /// <returns></returns>
        public IList<string> GetSourceDataOriginalListTxt(Verification_spokePersonIsNull spokePersonJoinIsNull, ElementExtractSource extractSource, IList<string> parentOriginalTxtList)
        {
            switch (extractSource.EES_SourceType.ToString())
            {
                case "1":
                    //文书段落对话内容
                    //进行验证一下要获取数据的实例是否为空，如果为空，则进行组合赋值；
                    Verification_spokePersonJoinIsNull();
                    spokePersonJoinIsNull();
                    if (!string.IsNullOrWhiteSpace(extractSource.EES_Where))
                    {
                        var whererow = dataContext.DataLayerAns.SpokePersonJoin.Select(extractSource.EES_Where).AsEnumerable<DataRow>().FirstOrDefault();
                        if (whererow != null)
                        {
                            //案件实体
                            var entiyModel = dataContext.DataLayerAns.Anjianshitis.FirstOrDefault(id => id.Anjianshitiid == whererow["Anjianshitiid"].ToString());
                            //案件实体对应的角色
                            var anjianjuese= dataContext.DataLayerAns.Anjianshitijueses.FirstOrDefault(ju => ju.Anjianshitiid == entiyModel?.Anjianshitiid);
                            //角色对应的发言人信息
                            var spokeTexts = dataContext.DataLayerAns.Fayanrenduanluoxinxis.Where(x => x.Jueseid == anjianjuese?.Jueseid).ToList();


                            var listtxt = spokeTexts.Where(parcode => parcode.Duanluobianhao.ToLower() == extractSource.EES_ParagraphName.ToLower());
                            if (listtxt != null)
                            {
                                return new List<string>() { string.Join("\r\n", listtxt.Select(txt => txt.Duanluoneirong)) };
                            }
                        }

                        //如果没合适的返回空内容
                        return new List<string>();
                    }
                    else
                    {
                        //如果配置的是对话内容，但是没有配置条件，说明当前配置有问题；
                        throw new ArgumentException("配置为对话内容，但是没有配置数据条件，错误。");
                    }

                case "2":
                    //文书段落内容
                    List<string> orglist = new List<string>();
                    if (extractSource.EES_ParagraphName.IndexOf(",") > -1)
                    {
                        List<string> codeList = extractSource.EES_ParagraphName.Split(',').ToList();
                        foreach (var code in codeList)
                        {
                            orglist.AddRange(dataContext.DataLayerAns.Duanluos.Where(par => par.Duanluobianhao.ToLower() == code.ToLower()).Select(mo => mo.Duanluoneirong).ToList());
                        }
                    }
                    else
                    {
                        orglist.AddRange(dataContext.DataLayerAns.Duanluos.Where(par => par.Duanluobianhao.ToLower() == extractSource.EES_ParagraphName.ToLower()).Select(mo => mo.Duanluoneirong).ToList());
                    }

                    return new List<string>() { string.Join("\r\n", orglist) };
                case "3":
                    //原文数据
                    return parentOriginalTxtList;
                case "4":
                    //全文内容
                    return new List<string>() { dataContext.DataLayerAns.FengXiCanShu.Juanzongneirong };
                default:
                    throw new ArgumentOutOfRangeException("EES_SourceType参数内容操作正常解析范围！应该为1-4");
            }
        }

        /// <summary>
        /// 案件要素中心数据源配置获取
        /// </summary>
        /// <param name="extractSource">数据源表配置对象</param>
        /// <param name="parentOriginalTxt">父级原文内容</param>
        /// <returns></returns>
        public string GetSourceDataOriginalTxt(ElementExtractSource extractSource, string parentOriginalTxt)
        {
            switch (Convert.ToInt32(extractSource.EES_SourceType))
            {
                case 1:
                    //文书段落对话内容
                    //进行验证一下要获取数据的实例是否为空，如果为空，则进行组合赋值；
                    Verification_spokePersonJoinIsNull();
                    if (!string.IsNullOrWhiteSpace(extractSource.EES_Where))
                    {
                        var whererow = dataContext.DataLayerAns.SpokePersonJoin.Select(extractSource.EES_Where).AsEnumerable<DataRow>().FirstOrDefault();
                        if (whererow != null)
                        {
                            var entiyModel = dataContext.DataLayerAns.ElementSpokeList.FirstOrDefault(id => id.ES_ID == whererow["ES_ID"].ToString());
                            var spokeTexts = dataContext.DataLayerAns.SpokeTextDialogue.Where(x => x.SpokesmanID == entiyModel?.ES_ID).ToList();
                            var listtxt = spokeTexts.Where(parcode => parcode.ParagraphCode.ToLower() == extractSource.EES_ParagraphName.ToLower());
                            if (listtxt != null)
                            {
                                return string.Join("\r\n", listtxt.Select(txt => txt.OriginalText));
                            }
                        }

                        //如果没合适的返回空内容
                        return "";
                    }
                    else
                    {
                        //如果配置的是对话内容，但是没有配置条件，说明当前配置有问题；
                        throw new ArgumentException("配置为对话内容，但是没有配置数据条件，错误。");
                    }

                case 2:
                    //文书段落内容
                    var orglist = dataContext.DataLayerAns.Duanluos.Where(par => par.Duanluobianhao.ToLower() == extractSource.EES_ParagraphName.ToLower()).Select(mo => mo.Duanluoneirong);

                    return string.Join("\r\n", orglist);
                case 3:
                    //原文数据
                    return parentOriginalTxt;
                case 4:
                    //全文内容
                    return dataContext.DataLayerAns.FengXiCanShu.Juanzongneirong; ;
                default:
                    throw new ArgumentOutOfRangeException("EES_SourceType参数内容操作正常解析范围！应该为1-4");
            }
        }




        /// <summary>
        /// 进行判断动态数据层中spokePersonJoin是否为空
        /// </summary>
        private void Verification_spokePersonJoinIsNull()
        {
            if (dataContext.DataLayerAns.SpokePersonJoin == null)
            {
                DataTable dataTable = new DataTable();
                DataColumn column = new DataColumn();
                column.ColumnName = "Anjianshitiid";
                column.DataType = Type.GetType("System.String");
                dataTable.Columns.Add(column);

                column = new DataColumn();
                column.ColumnName = "Mingcheng";
                column.DataType = Type.GetType("System.String");
                dataTable.Columns.Add(column);

                column = new DataColumn();
                column.ColumnName = "Bieming";
                column.DataType = Type.GetType("System.String");
                dataTable.Columns.Add(column);

                column = new DataColumn();
                column.ColumnName = "Jiancheng";
                column.DataType = Type.GetType("System.String");
                dataTable.Columns.Add(column);

                column = new DataColumn();
                column.ColumnName = "Juesesusongdiwei";
                column.DataType = Type.GetType("System.String");
                dataTable.Columns.Add(column);

                column = new DataColumn();
                column.ColumnName = "Fayanrenmingcheng";
                column.DataType = Type.GetType("System.String");
                dataTable.Columns.Add(column);

                column = new DataColumn();
                column.ColumnName = "Juesemingcheng";
                column.DataType = Type.GetType("System.String");
                dataTable.Columns.Add(column);

                column = new DataColumn();
                column.ColumnName = "Zhuzhangfangleixing";
                column.DataType = Type.GetType("System.String");
                dataTable.Columns.Add(column);

                //如果动态数据层中，人员角色信息为空，则需要进行组合赋值
                foreach (var shiti in dataContext.DataLayerAns.Anjianshitis)
                {
                    foreach (var juese in shiti.Anjianshitijueses)
                    {
                        DataRow row = dataTable.NewRow();
                        row["Anjianshitiid"] = shiti.Anjianshitiid;
                        row["Mingcheng"] = shiti.Mingcheng;
                        row["Bieming"] = shiti.Bieming;
                        row["Jiancheng"] = shiti.Jiancheng;
                        row["Juesesusongdiwei"] = juese.Juesesusongdiwei;
                        row["Fayanrenmingcheng"] = juese.Fayanrenmingcheng;
                        row["Juesemingcheng"] = juese.Juesemingcheng;
                        row["Zhuzhangfangleixing"] = juese.Zhuzhangfangleixing;
                        dataTable.Rows.Add(row);
                    }
                }

                //将当前拼接值进行赋值给集合对象，以便于下一次使用
                dataContext.DataLayerAns.SpokePersonJoin = dataTable;
            }
        }






    }
}
